package Dao;

import Bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;
@Repository
public class UserDaoImpl implements UserDao{

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public User selectUserByUserName(String userName) {

        String sql = "select * from user where username = ?";
        List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class)
                , userName);

        if (list.size() == 0){
            return null;
        }

        return list.get(0);
    }

    @Override
    public int insertUser(User user) {

        String sql = "insert into user (username,password,role) values(?,?,?)";
        int i = jdbcTemplate.update(sql,
                user.getUsername(),
                user.getPassword(),
                user.getRole());

        return i;
    }






}